package com.controller;

import com.domain.*;
import com.service.ResumeService;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ResumeService resumeService;

    @RequestMapping(value = "/loginStudent" ,produces = "text/html;charset=UTF-8")
    public ModelAndView loginStudent(User user, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (user.getType() == 2) {
            List<Student> students = studentService.login(user.getUsername(), user.getPassword());
            session.setAttribute("user", students.get(0));
            modelAndView.addObject("first", students);
            modelAndView.setViewName("first");
            return modelAndView;
        } else if (user.getType() == 1) {
            List<Teacher> teachers = studentService.tlogin(user.getUsername(), user.getPassword());
            session.setAttribute("audit", teachers.get(0));
            modelAndView.addObject("firsttwo", studentService.clist());
            modelAndView.addObject("teacher", teachers);
            modelAndView.setViewName("teacher");
            return modelAndView;
        } else if (user.getType() == 3) {
            modelAndView.addObject("company",studentService.clogin(user.getUsername(), user.getPassword()));
            List<Resume> resumes = resumeService.findAll(Integer.valueOf(user.getUsername()));
            modelAndView.addObject("resumes", resumes);
            modelAndView.setViewName("company");
            return modelAndView;
        }
          return null;
}

       @RequestMapping("/apply")
    public ModelAndView apply(){
           ModelAndView modelAndView = new ModelAndView();
           modelAndView.addObject("apply",studentService.apply());
           modelAndView.setViewName("apply");
           return modelAndView;
       }



}
